package com.wondersgroup.fjzlpt.org.dao.impl.tdgl;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.org.dao.inter.tdgl.TdryDao;

@Repository
public class TdryDaoImpl extends BaseJTDAOImpl implements TdryDao {

	@Override
	@Autowired
	@Qualifier("TB_TDGL_TDRY")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Override
	public int updateTDJS(Map<String, String> map) {
		StringBuffer sqlBuf = new StringBuffer("update ").append(this.tb.getTableName());
		sqlBuf.append(" set TDJS=? where TDID=? and USERID = ?");
		SQLBean bean = new SQLBean(sqlBuf.toString(),map.get("TDJS").toString(),map.get("TDID"),map.get("USERID"));
		return this.update(bean);
	}

	@Override
	public JSONArray queryTdcylbAll(Map<String, String> map) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append("select a.*,b.*,'old' as OLd,(select YLJGMC from TB_ZD_YLJGXX where YLJGDM=b.YLJGDM) YLJGMC,\n");
		sqlBuf.append("(select XMMC from tb_zy_zc where XMDM = b.zc) ZCMC,");
		sqlBuf.append("(select KSMC from TB_ZD_KSXX where PTJGID=b.PTJGID and KSDM = b.KSDM) KSMC \n");
		sqlBuf.append("from  TB_TDGL_TDRY a left join TB_ZD_YLJGRYXX b on a.USERID=b.USERID where a.TDID = ? order by a.TDJS");
		
		SQLBean bean = new SQLBean(sqlBuf.toString(),map.get("TDID"));
		return this.queryList(bean);
	}

	@Override
	public void deleteTdrys(String TDID, JSONArray userArr) {
		StringBuffer userIds = new StringBuffer();
		for(int i=0;i<userArr.size();i++){
			JSONObject item = userArr.getJSONObject(i);
			userIds.append(item.get("USERID")).append(";");
		}
		
		String userIdsStr = "('" + userIds.substring(0, userIds.length()-1).replace(";", "','") + "')";
		StringBuffer sqlBuf = new StringBuffer("delete from TB_TDGL_TDRY where TDID=? and USERID in").append(userIdsStr);
		
		SQLBean bean = new SQLBean(sqlBuf.toString(),TDID);
		super.update(bean);
	}

	@Override
	public boolean isExist(Map<String, String> map) {
		SQLBean bean = new SQLBean("select count(TDID) from TB_TDGL_TDRY where TDID = ? and USERID = ?",map.get("TDID"),map.get("USERID"));
		return super.isExist(bean);
	}

	/**
	 * 根据团队ID查询团队长USERID
	 * @param TDID
	 * @return
	 */
	@Override
	public String queryTdzUseridByTdid(String TDID) {
		StringBuffer sqlBuf = new StringBuffer("select USERID from ").append(this.tb.getTableName())
				.append(" where TDJS = 1 and TDID = ? ");
		SQLBean bean = new SQLBean(sqlBuf.toString() ,TDID);
		JSONObject userObj = super.queryOne(bean);
		return userObj.getString("USERID");
	}

	/**
	 * 根据团队长ID查询团队长角色
	 * @param TDZID
	 * @return
	 */
	@Override
	public String queryTdzUserRoleByTdzid(String TDZID) {
		StringBuffer sqlBuf = new StringBuffer("select ROLEID from TB_AUTH_USER_ROLE").append(" where USERID = ? ");
		SQLBean bean = new SQLBean(sqlBuf.toString() ,TDZID);
		JSONObject userObj = super.queryOne(bean);
		return userObj.getString("ROLEID");
	}

	/**
	 * 将用户的角色类型改为团队长
	 * @param USERID
	 * @param ROLEID
	 * @return
	 */
	public void updateTdzRole(String USERID,String ROLEID){
		SQLBean bean = new SQLBean("update TB_AUTH_USER_ROLE set ROLEID = ? where USERID = ?",ROLEID,USERID);
		super.update(bean);
	}
	
}
